﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using Microsoft.SharePoint.WebControls;
//using Microsoft.SharePoint;
//using AjaxControlToolkit;
//using Sapiens.SharePoint.Forms;
//using Sapiens.Core.Controls;

//namespace Sapiens.SharePoint.Forms
//{
//    /// <summary>
//    /// Extended <see cref="BaseForm"/> with a <see cref="TabContainer"/>.
//    /// </summary>
//    public class TabBaseForm : BaseForm
//    {
//        /// <summary>
//        /// The cookie name to store the active tab index
//        /// </summary>
//        public const string ACTIVE_TAB_INDEX_COOKIENAME = "ActiveTabIndex";

//        /// <summary>
//        /// The querystring parameter name to store the active tab index
//        /// </summary>
//        public const string SELECTED_TAB_QUERYSTRING = "SelectedTab";
        
//        /// <summary>
//        /// The tab container of the form
//        /// </summary>
//        protected TabContainer TabContainer = new TabContainer();

//        /// <summary>
//        /// The first tab panel that contains the <see cref="ListFieldIterator"/>
//        /// </summary>
//        protected TabPanel MetadataPanel = new TabPanel();

//        /// <summary>
//        /// Gets called when the form is initialized
//        /// </summary>
//        /// <param name="e"></param>
//        protected override void OnInit(EventArgs e)
//        {
//            // Problem when following a link from a lookup field to a tabbed form with a querystring parameter
//            // RootFolder=* (SPException inside TabPanel rendering)
//            if (Request.QueryString.AllKeys.Contains("RootFolder") && Request.QueryString["RootFolder"] == "*")
//            {
//                Response.Redirect(Request.Url.OriginalString.Replace("&RootFolder=*", "") ,true);
//            }
//            base.OnInit(e);
//        }

//        /// <summary>
//        /// Sets up the controls of the form.
//        /// </summary>
//        protected override void CreateChildControls()
//        {
//            Controls.Add(new SafeScriptManager());
//            // add jQuery
//            ScriptManager.RegisterClientScriptInclude(this, GetType(), "jQuery 1.3.2", Page.ResolveClientUrl("~/_layouts/scripts/jQuery/jquery-1.3.2.js"));
//            ScriptManager.RegisterClientScriptInclude(this, GetType(), "jQuery Cookie Plugin", Page.ResolveClientUrl("~/_layouts/scripts/jQuery/jquery.cookie.js"));
//            // add a link to the css for the tabs
//            ScriptManager.RegisterClientScriptBlock(this, GetType(), "TabsCssTheme",
//                "<link rel=\"stylesheet\" href=\"~/_layouts/styles/AjaxTabsTechnoratiTheme.css\" type=\"text/css\" media=\"screen\" />", false);
//            // add a method that saves the active tab in a cookie
//            ScriptManager.RegisterClientScriptBlock(this, GetType(), "sapiens.at.sp.eb.Controls.TabBaseForm",
//                "var selectedTabChanged = function(sender, args) {\n" +
//                    "sender.get_clientStateField().value = sender.saveClientState();\n" +
//                    "SetCookie(\"" + ACTIVE_TAB_INDEX_COOKIENAME + "\", sender._activeTabIndex,\"\");\n" +
//                    "};", true);

//            base.CreateChildControls();

//            #region Set active tab
//            // Tries to keep the active tab
//            if (TabContainer.Tabs.Count > 0)
//            {
//                if (Request.QueryString.AllKeys.Contains(SELECTED_TAB_QUERYSTRING))
//                {
//                    int tabIndex = int.TryParse(Request.QueryString[SELECTED_TAB_QUERYSTRING], out tabIndex) ? tabIndex : 0;
//                    TabContainer.ActiveTabIndex = tabIndex;
//                }
//                else
//                {
//                    TabContainer.ActiveTabIndex = 0;
//                    if (Page.Request.Cookies.AllKeys.Contains(ACTIVE_TAB_INDEX_COOKIENAME))
//                    {
//                        if (Page.IsPostBack && !string.IsNullOrEmpty(Page.Request.Cookies[ACTIVE_TAB_INDEX_COOKIENAME].Value))
//                        {
//                            var activeTabIndex = int.Parse(Page.Request.Cookies[ACTIVE_TAB_INDEX_COOKIENAME].Value);
//                            if (activeTabIndex < TabContainer.Tabs.Count)
//                            {
//                                TabContainer.ActiveTabIndex = activeTabIndex;
//                            }
//                        }
//                        else
//                        {
//                            Response.Cookies.Remove(ACTIVE_TAB_INDEX_COOKIENAME);
//                        }
//                    }
//                }
//            }
//            #endregion

//            TabContainer.OnClientActiveTabChanged = "selectedTabChanged";
//        }
//        /// <summary>
//        /// Gets called if the page is not valid. Highlights every tab, that has an invalid validator on it.
//        /// </summary>
//        protected override void OnInvalidSave()
//        {
//            var tabs = new List<TabPanel>(TabContainer.Tabs.Cast<TabPanel>());
//            foreach (IValidator validator in Page.Validators.OfType<IValidator>())
//            {
//                if (!validator.IsValid)
//                {
//                    var tab = GetTabPanel(validator as Control);
//                    tabs.Remove(tab);
//                    if (!tab.HeaderText.StartsWith("<span style='color:red;'>"))
//                    {
//                        tab.HeaderText = string.Format("<span style='color:red;'>{0}</span>", tab.HeaderText);
//                    }
//                }
//            }
//            // unhighlight valid tabs in case they were invalid in a previous request
//            foreach (var tab in tabs)
//            {
//                tab.HeaderText = tab.HeaderText.Replace("<span style='color:red;'>", string.Empty).Replace("</span>", string.Empty);
//            }
//        }

//        /// <summary>
//        /// Tries to find the tab on which the given control lies.
//        /// </summary>
//        /// <param name="control"></param>
//        /// <returns></returns>
//        protected TabPanel GetTabPanel(Control control)
//        {
//            if (control == null) { return null; }
//            if (control is TabPanel) { return (TabPanel)control; }
//            return GetTabPanel(control.NamingContainer);
//        }
//    }
//}
